package com.huangj.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.huangj.entity.HomeInfoEntity;
import com.huangj.pro2_h2pop.HomeInfoWebActivity;
import com.huangj.pro2_h2pop.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HuangJ on 2016/10/5.21:06
 */
public class RoteChildAdapter extends BaseAdapter implements View.OnClickListener {

    private Context context;
    private List<HomeInfoEntity.DataBean.SectionsBean.ModelsBean.DaysBean.PointsBean> datas;
    private int n = -1;
    public RoteChildAdapter(Context context) {
        this.context = context;
        this.datas = new ArrayList<>();
    }
    public void setDatas(List<HomeInfoEntity.DataBean.SectionsBean.ModelsBean.DaysBean.PointsBean> datas){
        this.datas = datas;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int index = position;
        final ViewHolder viewHolder ;
        if (convertView!=null){
            viewHolder = (ViewHolder) convertView.getTag();
        }else {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_jdchildlv,null);
            viewHolder.chrote_tv = (TextView) convertView.findViewById(R.id.chrote_tv);
            viewHolder.chrote_tvbottom = (TextView) convertView.findViewById(R.id.chrote_tvbottom);
            viewHolder.chrote_tvtime = (TextView) convertView.findViewById(R.id.chrote_tvtime);
            viewHolder.chrote_summery = (TextView) convertView.findViewById(R.id.chrote_summery);
            viewHolder.chrote_iv = (ImageView) convertView.findViewById(R.id.chrote_iv);
            viewHolder.chrote_ll = (LinearLayout) convertView.findViewById(R.id.chrote_ll);
            viewHolder.chrote_rl = (RelativeLayout) convertView.findViewById(R.id.chrote_rl);
            convertView.setTag(viewHolder);
        }
        viewHolder.chrote_tv.setText(datas.get(position).getInspiration_activity().getTopic());
        viewHolder.chrote_tvbottom.setText(datas.get(position).getInspiration_activity().getDestination().getName());
        viewHolder.chrote_tvtime.setText("建议游玩"+datas.get(position).getInspiration_activity().getVisit_tip());
        viewHolder.chrote_summery.setText(datas.get(position).getInspiration_activity().getIntroduce());
        Glide.with(context).load(datas.get(position).getInspiration_activity().getPhoto().getPhoto_url())
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .crossFade()
                .into(viewHolder.chrote_iv);
        //点击图片跳转；
        viewHolder.chrote_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "点击了图片", Toast.LENGTH_SHORT).show();
                String h5_url = datas.get(index).getPoi().getH5_url();
                Intent intent = new Intent(context, HomeInfoWebActivity.class);
                intent.putExtra("h5_url",h5_url);
                context.startActivity(intent);
            }
        });

        //相对布局的监听事件；
        viewHolder.chrote_rl.setOnClickListener(this);
        viewHolder.chrote_rl.setTag(position);
        viewHolder.chrote_ll.setVisibility(n == position ? View.VISIBLE:View.GONE);
//            @Override
//            public void onClick(View v) {
//                if (viewHolder.chrote_ll.getVisibility() == View.GONE){
//
//                        viewHolder.chrote_ll.setVisibility(View.VISIBLE);
//                    notifyDataSetChanged();
//                    }
//                        else{
//
//                        viewHolder.chrote_ll.setVisibility(View.GONE);
//                    notifyDataSetChanged();
//                    }
//            }


        return convertView;
    }

    @Override
    public void onClick(View v) {
        int rlTag = (int) v.getTag();
        if (n == rlTag){
            n = -1;
        }else {
            n = rlTag;
        }
        notifyDataSetChanged();
    }

    class ViewHolder{
        TextView chrote_tv,chrote_tvbottom,chrote_tvtime,chrote_summery;
        ImageView chrote_iv;
        LinearLayout chrote_ll;
        RelativeLayout chrote_rl;
    }
}
